URL = "ftp://ftp.remotesensing.org/pub/libtiff/tiff-%(VERSION)s.tar.gz"
VERSIONS = ["3.9.2"]
DEFAULT_VERSION = "3.9.2"
DEPENDS = ["zlib", "jpeg"]

# Windows: Custom NMakeBuilder, zlib support is configured in the nmake.opt config file
# Linux: Standard ConfigureMakeBuilder, not yet tested, no zlib support yet

import  os, shutil
from source.builders import NMakeBuilder, ConfigureMakeBuilder

#def gettiffdir(machine, directories):
#  return directories.enviro+"/"+"tiff-"+dotversion

class TIFFNMakeBuilder( NMakeBuilder ):
  """ subclass of ConfigureMakeBuilder with custom configure step """

  def configure(self):
    """add support for zlib and jpeg by changing the nmake.opt config file """


  #  import build_zlib
    #zlibdir=build_zlib.getzlibdir(self.machine, self.directories)
    zlibdir = self.dependencies["zlib"].targetpath()
    
    replacements = []
    replacements.append(( ".*#ZIP_SUPPORT	= 1", "ZIP_SUPPORT	= 1"))
    replacements.append(( ".*#ZLIBDIR 	= d:/projects/zlib-1.2.1", "ZLIBDIR 	= "+zlibdir))
    replacements.append(( ".*#ZLIB_INCLUDE	= -I\$\(ZLIBDIR\)", "ZLIB_INCLUDE	= -I$(ZLIBDIR)/include"))
    replacements.append(( ".*#ZLIB_LIB 	= \$\(ZLIBDIR\)/zlib.lib", "ZLIB_LIB 	= $(ZLIBDIR)/lib/zlib.lib" ))

    self.replaceLinesInFile( self.extractedpath+"\\nmake.opt", replacements )

  def makefile(self):
    return "Makefile.vc"

  def install(self):
    os.chdir(self.extractedpath)
    bindir = self.installpath() + "/bin"
    libdir = self.installpath() + "/lib"
    includedir = self.installpath() + "/include/"

    #dll binaries
    self.copyfile( "libtiff/libtiff.dll", bindir )
    #dynamic link libs
    self.copyfile( "libtiff/libtiff.lib", libdir )
    self.copyfile( "libtiff/libtiff_i.lib", libdir )
    #headers
    self.copypattern("libtiff", "^.*\\.h$", includedir )
    


class TIFFConfigureMakeBuilder( ConfigureMakeBuilder ):
  """ subclass of ConfigureMakeBuilder with custom configure params """

  def configureparameters(self):
    """ add support for zlib and jpeg by adding the parameters """
    parameters = []
    
    # zlib deps
    zlibmod = self.dependencies["zlib"]
    zlibdir = zlibmod.getlibdir()
    zincdir = zlibmod.getincludedir()
    parameters.append("--with-zlib-include-dir=" + zincdir)
    parameters.append("--with-zlib-lib-dir=" + zlibdir)
 
    # jpeg deps
    jpeglibmod = self.dependencies["jpeg"]
    jpeglibdir = jpeglibmod.getlibdir()
    jpegincdir = jpeglibmod.getincludedir()
    parameters.append("--with-jpeg-include-dir=" + jpegincdir)
    parameters.append("--with-jpeg-lib-dir=" + jpeglibdir)

    return parameters


def decider( machine, dirs ):
  """ build png for the machine in dirs """
  return {"Windows": TIFFNMakeBuilder, "Linux": TIFFConfigureMakeBuilder}[machine.platform]
  
BUILDER = decider
